package com.tdam2013.grupo01;

import java.util.List;

import com.tdam2013.grupo01.R;
import com.tdam2013.grupo01.HistoryItem;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListHistoryAdapter extends ArrayAdapter<HistoryItem> {

	   Context context;

	   public CustomListHistoryAdapter(Context context, int resourceId,
	           List<HistoryItem> items) {
	       super(context, resourceId, items);
	       this.context = context;
	   }

	   /*private view holder class*/
	   private class ViewHolder {
	       ImageView imageAction;
	       ImageView imageInOut;
	       TextView labelContacto;
	       TextView labelDateTime;
	   }

	   public View getView(int position, View convertView, ViewGroup parent) {
	       ViewHolder holder = null;
	       HistoryItem historyItem = getItem(position);

	       LayoutInflater mInflater = (LayoutInflater) context
	               .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	       if (convertView == null) {
	           convertView = mInflater.inflate(R.layout.historyitem, null);
	           holder = new ViewHolder();
	           holder.labelContacto = (TextView) convertView.findViewById(R.id.lblContactNameHistory);
	           holder.labelDateTime = (TextView) convertView.findViewById(R.id.lblDateTimeHistory);
	           holder.imageAction = (ImageView) convertView.findViewById(R.id.imgAction);
	           holder.imageInOut = (ImageView) convertView.findViewById(R.id.imgInOut);
	           convertView.setTag(holder);
	       } else
	       {
	           holder = (ViewHolder) convertView.getTag();
	       }
	       holder.labelContacto.setText(historyItem.getLabelContacto());
	       holder.labelDateTime.setText(historyItem.getLabelDateTime());
	       holder.imageAction.setImageResource(historyItem.getImageAction());
	       holder.imageInOut.setImageResource(historyItem.getImageInOut());

	       return convertView;
	   }
	}
